#include "PanelBill.h"
#include "ui_PanelBill.h"
#include <QTableWidgetItem>
#include <QDebug>

PanelBill::PanelBill(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PanelBill)
{
    ui->setupUi(this);

    QStringList hHeaderList = {"名称","当前值","默认值","最小值","最大值","步长","单位", "定值数据类型"}; // 7列

    ui->tableWidgetBill->setColumnCount(hHeaderList.count());

    ui->tableWidgetBill->setHorizontalHeaderLabels(hHeaderList);

    ui->tableWidgetBill->reset();

    connect(ui->pushButtonClearAll, &QPushButton::clicked, this, &PanelBill::slot_pushButtonClearAll_clicked);
}

PanelBill::~PanelBill()
{
    delete ui;
}

void PanelBill::showBillTables(QList<QStringList> tableInfoLists)
{
    ui->tableWidgetBill->setRowCount(tableInfoLists[0].count());
    for (int j = 0; j<tableInfoLists.count()-2; j++)
    {
        for (int i = 0; i<tableInfoLists[j].count(); i++)
        {
            QTableWidgetItem *tmpTableItem = new QTableWidgetItem;
            tmpTableItem->setText(tableInfoLists[j][i]);
            ui->tableWidgetBill->setItem(i, j, tmpTableItem);
        }
    }

    ui->tableWidgetGroupTitle->setRowCount(tableInfoLists.last().count());
    for (int i = 0; i<tableInfoLists[8].count(); i++)
    {
        QTableWidgetItem *tmpTableItem = new QTableWidgetItem;
        tmpTableItem->setText(tableInfoLists[8][i]);
        ui->tableWidgetGroupTitle->setItem(i,0 , tmpTableItem);
    }

    for (int i = 0; i<tableInfoLists[9].count(); i++)
    {
        QTableWidgetItem *tmpTableItem = new QTableWidgetItem;
        tmpTableItem->setText(tableInfoLists[9][i]);
        ui->tableWidgetGroupTitle->setItem(i,1 , tmpTableItem);
    }
    this->show();
}

 QVector< QVector<OneDataSetStruct> >  PanelBill::getNewDataSet()
{
    QStringList dataList, dataTypeList;
    OneDataSetStruct tmpDataSet;

    QVector< QVector<OneDataSetStruct> > allGroupDataSets; // 每组一个 dataSet list，存储全部组的数据集
    QVector<OneDataSetStruct> tmpDataSetList;
    QString tmpGroupNo;         // 组号
    for ( int i = 0; i<ui->tableWidgetBill->rowCount(); i++ )
    {
        OneDataSetStruct tmpData;
        // 遍历当前定值列，当遇到新的一组会有组号信息
        QTableWidgetItem *tmpItemValue = ui->tableWidgetBill->item(i, SETTIG_COLINDX);
        QString billValue = tmpItemValue->text();
        QTableWidgetItem *tmpItemDataType = ui->tableWidgetBill->item(i, DATATYPE_COLINDX);
        QString dataType =  tmpItemDataType->text();

        if ( billValue.contains(SPACE_) ) // 组名字信息行 "组号:02"
        {
            tmpGroupNo = billValue.split(':').last();
            if ( tmpDataSetList.count() != 0 )  // 不是表格第一行，就把当前行以上一组数据存入
            {
                allGroupDataSets.push_back(tmpDataSetList);    // 完成一组条目数据的获取
                tmpDataSetList.clear();     // 为存储这组清理空间
            }
            continue;
        }

        tmpData.m_ginGroup = tmpGroupNo;
        tmpData.m_ginNo = QString::number(i,16);
        tmpData.m_dataValue = billValue;
        tmpData.m_dataNum = 1;
        QString tmpTypeProtco;
        int tmpWidth;
        dataTypeToType(billValue, dataType, tmpTypeProtco, tmpWidth);
        tmpData.m_dataType = dataType;      // 待转换为协议类型符号
        tmpData.m_dataWidth = QString::number(tmpWidth);
        tmpDataSetList.append(tmpData);     // 添加一个数据元素到当前组数据集
    }

    return allGroupDataSets;
 }


void PanelBill::slot_recvParserResult(QStringList ContentsList, int columnIndex)       // 由 parser 解析出的类型序号来确认数据序列放在表格哪一个列
{
    qDebug() << "panel 接收到数据，准备刷新数据个数据" << ContentsList.count();
    qDebug() << "panel 接收到数据，准备刷新数据内容" << ContentsList;

    // 获取全部组的标题和组号
    if ( columnIndex == 0 )
    {
        QStringList groupNoes = ContentsList.mid(0,ContentsList.count()/2);
        QStringList titles = ContentsList.mid(ContentsList.count()/2, ContentsList.count()/2);

        setDataListToGroupTitleTable(groupNoes, 0);
        setDataListToGroupTitleTable(titles, 1);
    }
    // 量程的显示，首先需要将其中的最小值、最大值、步长 拆分 出3个 list
    // 显示默认值，和默认值数据类型
    else if ( columnIndex == 3 )
    {
        QStringList defaultValues = ContentsList.mid(0, ContentsList.count()/2);
        setDataListToTable(defaultValues, 2);
        QStringList dataTypes = ContentsList.mid(ContentsList.count()/2, ContentsList.count()/2);

        for (int i=0; i< dataTypes.count(); i++)
        {
            dataTypeToRealName(dataTypes[i]);
        }

        setDataListToTable(dataTypes, 7);

    }
    else if ( columnIndex == 4 )
    {
        QStringList minList, maxList, stepList;
        // 取出最小值
        minList = ContentsList.mid(0, ContentsList.count()/3);
        maxList = ContentsList.mid(ContentsList.count()/3, ContentsList.count()/3);
        stepList = ContentsList.mid(ContentsList.count() - ContentsList.count()/3, ContentsList.count()/3);

        setDataListToTable( minList, columnIndex -1 );
        setDataListToTable( maxList, columnIndex );
        setDataListToTable( stepList, columnIndex + 1 );
    }
    else
    {
        setDataListToTable(ContentsList, columnIndex - 1);
    }
}

QStringList PanelBill::slot_getModifiedBillValues()
{
    QStringList modifiedValues;

    for (int i=0; i<ui->tableWidgetBill->rowCount(); i++)
    {
        if( ui->tableWidgetBill->item(i,1) != nullptr )
        {
             // 设置新修改的定值列
            modifiedValues.append(ui->tableWidgetBill->item(i,1)->text());
        }
    }

    return modifiedValues;
}

void PanelBill::slot_pushButtonClearAll_clicked()
{
    ui->tableWidgetBill->clearContents();
    ui->tableWidgetBill->setRowCount(0);
    ui->tableWidgetGroupTitle->clearContents();
    ui->tableWidgetGroupTitle->setRowCount(0);
    ui->labelBillArea->clear();
}

void PanelBill::setDataListToTable(const QStringList &newDataList, int columnIndex)
{
    //在更新表格数据的前面插入标题

    if ( columnIndex == -2 )    // 需要显示的数据是定值区号
    {
        ui->labelBillArea->setText(newDataList[0]);
        return;
    }

    if ( (newDataList.count() > ui->tableWidgetBill->rowCount())  && columnIndex != 7 )
    {
        ui->tableWidgetBill->setRowCount(newDataList.count());
    }

    for (int i = 0; i<newDataList.count(); i++)
    {
        QTableWidgetItem *tmpTableItem = new QTableWidgetItem;
        tmpTableItem->setText(newDataList[i]);
        ui->tableWidgetBill->setItem(i, columnIndex, tmpTableItem);
    }

    //ui->tableWidgetBill->resizeColumnToContents(columnIndex);
    // 实时将表格宽度依据内容长度更
    ui->tableWidgetBill->resizeColumnToContents(columnIndex);
}

void PanelBill::setDataListToGroupTitleTable(const QStringList &newDataList, int colIndex)
{
    if ( (newDataList.count() > ui->tableWidgetGroupTitle->rowCount()) )
    {
        ui->tableWidgetGroupTitle->setRowCount(newDataList.count());
    }

    for (int i = 0; i<newDataList.count(); i++)
    {
        QTableWidgetItem *tmpTableItem = new QTableWidgetItem;
        tmpTableItem->setText(newDataList[i]);
        ui->tableWidgetGroupTitle->setItem(i, colIndex, tmpTableItem);
    }
}

void PanelBill::dataTypeToRealName(QString &numStr)
{
    if (numStr == "00")
    {
        qDebug() << "数据无所对应的类型";
        numStr = "未知";
    }
    else if (numStr == "01")
    {
        numStr =  "OS8ASCII";
    }
    else if (numStr == "03")
    {
        numStr =  "无符号整数";
    }
    else if (numStr == "04")
    {
        numStr = "整数";
    }
    else if( numStr == "06")
    {
        numStr =  "浮点数";     // IEEE 754 短实数
    }
    else if (numStr == "07")      // 数据类型为短实数
    {
        numStr = "短实数 ";
    }
    else if (numStr == "09")
    {
       numStr = "双点信息";        // bool 类型值
    }
    else if (numStr == "10")
    {
       numStr =  "单点信息";
    }
    else if (numStr == "0C")
    {
       numStr = "带品质描述的被测值";
    }
    else if (numStr == "13")
    {
        numStr = "带时标的报文";
    }
    else if (numStr == "18")
    {
        numStr = "数据结构";
    }
    else if (numStr == "CB")
    {
        numStr = "带7字节的时标报文";
    }
    else if (numStr == "CC")
    {
        numStr = "带相对时间的7字节时标报文";
    }
    else if(numStr == "00")
    {
        numStr =  "无所确定的类型";
    }
    else
    {
        numStr = "未来类型";
    }
}

void PanelBill::dataTypeToType(QString &data , QString &dataTypeStr, QString &type, int& width)
{
    int dataStrCount = data.count();

    if ("OS8ASCII" == dataTypeStr) {        // 设备名称，组标题之类使用， 长度根据实际值的字符个数来，一个字符一个字节
        type = "01";                        // 保护可以接受汉字字符输入， 定值
        width = dataStrCount;
    }
    else if ("无符号整数" == dataTypeStr ) {
        type =  "04";
        width = 2* dataStrCount;
    }
    else if ("整数" == dataTypeStr ){
        type = "03";
        width = 4 * dataStrCount;
    }
    else if ("浮点数" == dataTypeStr){
        type =  "06";
        width = 4 * dataStrCount;
    }
    else if ("短实数" == dataTypeStr){
        type =  "07";
        width = 8 * dataStrCount;
    }
    else if("单点信息" == dataTypeStr){
        type = "10";
        width = dataStrCount;
    }
    else if("双点信息" == dataTypeStr)
    {
        type =  "09";
        width = 2 * dataStrCount;
    }
    else {
        type = "99";
        width = 0;
    }
}

void PanelBill::setGroupTitleSpan(int rowIndx)
{
    ui->tableWidgetBill->setSpan(rowIndx,0,1,8);
}

void PanelBill::insertData(QStringList &newDataList, int colIndex, int startRowIndex)
{
    for (int i = 0; i<newDataList.count(); i++)
    {
        QTableWidgetItem *tmpTableItem = new QTableWidgetItem;
        tmpTableItem->setText(newDataList[i]);
        ui->tableWidgetBill->setItem(startRowIndex+i, colIndex, tmpTableItem);
    }
}

bool PanelBill::checkTableDataSets()
{
    int titleRowNum = ui->tableWidgetGroupTitle->rowCount();
    int billValueRowNum = ui->tableWidgetBill->columnCount();

    if (titleRowNum==0 || billValueRowNum==0) {
        return false;
    }

   // 判断最后一行是否为空
    for (int i=0; i<billValueRowNum; i++) {
        if (ui->tableWidgetBill->item(billValueRowNum, i)== nullptr) {
            return false;
        }
    }

    return true;
}


